package com.jblv.frame.common.utils;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * @author happyonion.yangtao
 * @version 1.0
 * @title PropertiesUtils
 * @projectName jblv-frame
 * @packageName com.jblv.common.utils
 * @description PropertiesUtils
 * @date Create at 2020/5/7 10:57
 * @since jdk1.8
 */


public class PropertiesUtils {
    public PropertiesUtils() {
    }

    public static Map<String, String> getProperties(String filePath) throws IOException {
        if (filePath != null && filePath.length() != 0) {
            InputStream in = null;
            Properties pps = new Properties();

            try {
                in = new BufferedInputStream(new FileInputStream(filePath));
                pps.load(in);
                Map<String, String> resultMap = new HashMap();
                Enumeration enumeration = pps.propertyNames();

                while(enumeration.hasMoreElements()) {
                    String strKey = (String)enumeration.nextElement();
                    String strValue = pps.getProperty(strKey);
                    resultMap.put(strKey, strValue);
                }

                return resultMap;
            } catch (IOException var14) {
                System.out.println(new Date() + " : 未找到文件 " + filePath + "");
                throw var14;
            } finally {
                try {
                    if (in != null) {
                        in.close();
                    }
                } catch (Exception var13) {
                    var13.printStackTrace();
                }

            }
        } else {
            return null;
        }
    }

    public static String getPropertie(String filePath, String propertieName) throws IOException {
        Map<String, String> configMap = getProperties(filePath);
        return configMap != null && !configMap.isEmpty() ? (String)configMap.get(propertieName) : null;
    }
}

